<html lang="en-US">

<head>
    
<title>for-loop 与 json.Unmarshal 性能分析概要 - 花落雨忧</title>

<meta property="og:title" content="for-loop 与 json.Unmarshal 性能分析概要 - 花落雨忧">



    



    
    <meta property="description" content="在项目中，常常会遇到循环交换赋值的数据处理场景，尤其是 RPC，数据交互格式要转为 Protobuf，赋值是无法避免的。一般会有如下几种做法：
[&amp;hellip;] 这时候又面临 “选择困难症”，用哪个好？又想代码量少，又担心性能有没有影响啊&amp;hellip;
为了弄清楚这个疑惑，接下来将分别编写三种使用场景。来简单看看它们的性能情况，看看谁更 “好”
[&amp;hellip;] ... type &amp;hellip;">
    <meta property="og:description" content="在项目中，常常会遇到循环交换赋值的数据处理场景，尤其是 RPC，数据交互格式要转为 Protobuf，赋值是无法避免的。一般会有如下几种做法：
[&amp;hellip;] 这时候又面临 “选择困难症”，用哪个好？又想代码量少，又担心性能有没有影响啊&amp;hellip;
为了弄清楚这个疑惑，接下来将分别编写三种使用场景。来简单看看它们的性能情况，看看谁更 “好”
[&amp;hellip;] ... type &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.asia/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.asia/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.asia/js/jquery.min.js"></script>




<link href="https://www.lican.asia/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>

        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/posts/go/talk/2019-02-17-for-loop-json-unmarshal/">for-loop 与 json.Unmarshal 性能分析概要</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2019/02/17</a>
                                            
                                            
                                            
                                            By 
                                            <a class="author" href="javascript:;">煎鱼</a></div>
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <p>在项目中，常常会遇到循环交换赋值的数据处理场景，尤其是 RPC，数据交互格式要转为 Protobuf，赋值是无法避免的。一般会有如下几种做法：</p>
<ul>
<li>for</li>
<li>for range</li>
<li>json.Marshal/Unmarshal</li>
</ul>
<p>这时候又面临 “选择困难症”，用哪个好？又想代码量少，又担心性能有没有影响啊&hellip;</p>
<p>为了弄清楚这个疑惑，接下来将分别编写三种使用场景。来简单看看它们的性能情况，看看谁更 “好”</p>
<h2 id="功能代码">功能代码</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Person</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Name</span>   <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;name&#34;`</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Age</span>    <span style="color:#66d9ef">int</span>    <span style="color:#e6db74">`json:&#34;age&#34;`</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Avatar</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;avatar&#34;`</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Type</span>   <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;type&#34;`</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">AgainPerson</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Name</span>   <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;name&#34;`</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Age</span>    <span style="color:#66d9ef">int</span>    <span style="color:#e6db74">`json:&#34;age&#34;`</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Avatar</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;avatar&#34;`</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Type</span>   <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;type&#34;`</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">MAX</span> = <span style="color:#ae81ff">10000</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">InitPerson</span>() []<span style="color:#a6e22e">Person</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">persons</span> []<span style="color:#a6e22e">Person</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#a6e22e">MAX</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">persons</span> = append(<span style="color:#a6e22e">persons</span>, <span style="color:#a6e22e">Person</span>{
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">Name</span>:   <span style="color:#e6db74">&#34;EDDYCJY&#34;</span>,
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">Age</span>:    <span style="color:#a6e22e">i</span>,
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">Avatar</span>: <span style="color:#e6db74">&#34;https://github.com/EDDYCJY&#34;</span>,
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">Type</span>:   <span style="color:#e6db74">&#34;Person&#34;</span>,
</span></span><span style="display:flex;"><span>		})
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">persons</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">ForStruct</span>(<span style="color:#a6e22e">p</span> []<span style="color:#a6e22e">Person</span>, <span style="color:#a6e22e">count</span> <span style="color:#66d9ef">int</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#a6e22e">count</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">_</span> = <span style="color:#a6e22e">i</span>, <span style="color:#a6e22e">p</span>[<span style="color:#a6e22e">i</span>]
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">ForRangeStruct</span>(<span style="color:#a6e22e">p</span> []<span style="color:#a6e22e">Person</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span>, <span style="color:#a6e22e">v</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">p</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">_</span> = <span style="color:#a6e22e">i</span>, <span style="color:#a6e22e">v</span>
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">JsonToStruct</span>(<span style="color:#a6e22e">data</span> []<span style="color:#66d9ef">byte</span>, <span style="color:#a6e22e">againPerson</span> []<span style="color:#a6e22e">AgainPerson</span>) ([]<span style="color:#a6e22e">AgainPerson</span>, <span style="color:#66d9ef">error</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">json</span>.<span style="color:#a6e22e">Unmarshal</span>(<span style="color:#a6e22e">data</span>, <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">againPerson</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">againPerson</span>, <span style="color:#a6e22e">err</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">JsonIteratorToStruct</span>(<span style="color:#a6e22e">data</span> []<span style="color:#66d9ef">byte</span>, <span style="color:#a6e22e">againPerson</span> []<span style="color:#a6e22e">AgainPerson</span>) ([]<span style="color:#a6e22e">AgainPerson</span>, <span style="color:#66d9ef">error</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">jsonIter</span> = <span style="color:#a6e22e">jsoniter</span>.<span style="color:#a6e22e">ConfigCompatibleWithStandardLibrary</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">jsonIter</span>.<span style="color:#a6e22e">Unmarshal</span>(<span style="color:#a6e22e">data</span>, <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">againPerson</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">againPerson</span>, <span style="color:#a6e22e">err</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="测试代码">测试代码</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">BenchmarkForStruct</span>(<span style="color:#a6e22e">b</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">testing</span>.<span style="color:#a6e22e">B</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">person</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">InitPerson</span>()
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">count</span> <span style="color:#f92672">:=</span> len(<span style="color:#a6e22e">person</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">b</span>.<span style="color:#a6e22e">ResetTimer</span>()
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#a6e22e">b</span>.<span style="color:#a6e22e">N</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">ForStruct</span>(<span style="color:#a6e22e">person</span>, <span style="color:#a6e22e">count</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">BenchmarkForRangeStruct</span>(<span style="color:#a6e22e">b</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">testing</span>.<span style="color:#a6e22e">B</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">person</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">InitPerson</span>()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">b</span>.<span style="color:#a6e22e">ResetTimer</span>()
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#a6e22e">b</span>.<span style="color:#a6e22e">N</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">ForRangeStruct</span>(<span style="color:#a6e22e">person</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">BenchmarkJsonToStruct</span>(<span style="color:#a6e22e">b</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">testing</span>.<span style="color:#a6e22e">B</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">var</span> (
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">person</span> = <span style="color:#a6e22e">InitPerson</span>()
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">againPersons</span> []<span style="color:#a6e22e">AgainPerson</span>
</span></span><span style="display:flex;"><span>	)
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">data</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">json</span>.<span style="color:#a6e22e">Marshal</span>(<span style="color:#a6e22e">person</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">b</span>.<span style="color:#a6e22e">Fatalf</span>(<span style="color:#e6db74">&#34;json.Marshal err: %v&#34;</span>, <span style="color:#a6e22e">err</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">b</span>.<span style="color:#a6e22e">ResetTimer</span>()
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#a6e22e">b</span>.<span style="color:#a6e22e">N</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">JsonToStruct</span>(<span style="color:#a6e22e">data</span>, <span style="color:#a6e22e">againPersons</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">BenchmarkJsonIteratorToStruct</span>(<span style="color:#a6e22e">b</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">testing</span>.<span style="color:#a6e22e">B</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">var</span> (
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">person</span> = <span style="color:#a6e22e">InitPerson</span>()
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">againPersons</span> []<span style="color:#a6e22e">AgainPerson</span>
</span></span><span style="display:flex;"><span>	)
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">data</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">json</span>.<span style="color:#a6e22e">Marshal</span>(<span style="color:#a6e22e">person</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">b</span>.<span style="color:#a6e22e">Fatalf</span>(<span style="color:#e6db74">&#34;json.Marshal err: %v&#34;</span>, <span style="color:#a6e22e">err</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">b</span>.<span style="color:#a6e22e">ResetTimer</span>()
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#a6e22e">b</span>.<span style="color:#a6e22e">N</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">JsonIteratorToStruct</span>(<span style="color:#a6e22e">data</span>, <span style="color:#a6e22e">againPersons</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="测试结果">测试结果</h2>
<pre tabindex="0"><code>BenchmarkForStruct-4              	  500000	      3289 ns/op	       0 B/op	       0 allocs/op
BenchmarkForRangeStruct-4         	  200000	      9178 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonToStruct-4           	     100	  19173117 ns/op	 2618509 B/op	   40036 allocs/op
BenchmarkJsonIteratorToStruct-4   	     300	   4116491 ns/op	 3694017 B/op	   30047 allocs/op
</code></pre><p>从测试结果来看，性能排名为：for &lt; for range &lt; json-iterator &lt; encoding/json。接下来我们看看是什么原因导致了这样子的排名？</p>
<h2 id="性能对比">性能对比</h2>
<p><img src="https://s2.ax1x.com/2020/02/27/3wuywR.png" alt="image"></p>
<h3 id="for-loop">for-loop</h3>
<p>在测试结果中，<code>for range</code> 在性能上相较 <code>for</code> 差。这是为什么呢？在这里我们可以参见 <code>for range</code> 的 <a href="https://github.com/gcc-mirror/gcc/blob/master/gcc/go/gofrontend/statements.cc">实现</a>，伪实现如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">for_temp</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">len_temp</span> <span style="color:#f92672">:=</span> len(<span style="color:#a6e22e">for_temp</span>)
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">for</span> <span style="color:#a6e22e">index_temp</span> = <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">index_temp</span> &lt; <span style="color:#a6e22e">len_temp</span>; <span style="color:#a6e22e">index_temp</span><span style="color:#f92672">++</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">value_temp</span> = <span style="color:#a6e22e">for_temp</span>[<span style="color:#a6e22e">index_temp</span>]
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">index</span> = <span style="color:#a6e22e">index_temp</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">value</span> = <span style="color:#a6e22e">value_temp</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">original</span> <span style="color:#a6e22e">body</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>通过分析伪实现，可得知 <code>for range</code> 相较 <code>for</code> 多做了如下事项</p>
<h4 id="expression">Expression</h4>
<pre tabindex="0"><code>RangeClause = [ ExpressionList &#34;=&#34; | IdentifierList &#34;:=&#34; ] &#34;range&#34; Expression .
</code></pre><p>在循环开始之前会对范围表达式进行求值，多做了 “解” 表达式的动作，得到了最终的范围值</p>
<h4 id="copy">Copy</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">value_temp</span> = <span style="color:#a6e22e">for_temp</span>[<span style="color:#a6e22e">index_temp</span>]
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">index</span> = <span style="color:#a6e22e">index_temp</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">value</span> = <span style="color:#a6e22e">value_temp</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span></code></pre></div><p>从伪实现上可以得出，<code>for range</code> 始终使用<strong>值拷贝</strong>的方式来生成循环变量。通俗来讲，就是在每次循环时，都会对循环变量重新分配</p>
<h4 id="小结">小结</h4>
<p>通过上述的分析，可得知其比 <code>for</code> 慢的原因是 <code>for range</code> 有额外的性能开销，主要为<strong>值拷贝的动作</strong>导致的性能下降。这是它慢的原因</p>
<p>那么其实在 <code>for range</code> 中，我们可以使用 <code>_</code> 和 <code>T[i]</code> 也能达到和 <code>for</code> 差不多的性能。但这可能不是 <code>for range</code> 的设计本意了</p>
<h3 id="jsonmarshalunmarshal">json.Marshal/Unmarshal</h3>
<h4 id="encodingjson">encoding/json</h4>
<p>json 互转是在三种方案中最慢的，这是为什么呢？</p>
<p>众所皆知，官方的 <code>encoding/json</code> 标准库，是通过大量反射来实现的。那么 “慢”，也是必然的。可参见下述代码：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">newTypeEncoder</span>(<span style="color:#a6e22e">t</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Type</span>, <span style="color:#a6e22e">allowAddr</span> <span style="color:#66d9ef">bool</span>) <span style="color:#a6e22e">encoderFunc</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">switch</span> <span style="color:#a6e22e">t</span>.<span style="color:#a6e22e">Kind</span>() {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Bool</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">boolEncoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Int</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Int8</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Int16</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Int32</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Int64</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">intEncoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Uint</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Uint8</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Uint16</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Uint32</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Uint64</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Uintptr</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">uintEncoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Float32</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">float32Encoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Float64</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">float64Encoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">String</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">stringEncoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Interface</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">interfaceEncoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Struct</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">newStructEncoder</span>(<span style="color:#a6e22e">t</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Map</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">newMapEncoder</span>(<span style="color:#a6e22e">t</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Slice</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">newSliceEncoder</span>(<span style="color:#a6e22e">t</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Array</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">newArrayEncoder</span>(<span style="color:#a6e22e">t</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">Ptr</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">newPtrEncoder</span>(<span style="color:#a6e22e">t</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">default</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">unsupportedTypeEncoder</span>
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>既然官方的标准库存在一定的 “问题”，那么有没有其他解决方法呢？目前在社区里，大多为两类方案。如下：</p>
<ul>
<li>预编译生成代码（提前确定类型），可以解决运行时的反射带来的性能开销。缺点是增加了预生成的步骤</li>
<li>优化序列化的逻辑，性能达到最大化</li>
</ul>
<p>接下来的实验，我们用第二种方案的库来测试，看看有没有改变。另外也推荐大家了解如下项目：</p>
<ul>
<li><a href="https://github.com/json-iterator/go">json-iterator/go</a></li>
<li><a href="https://github.com/mailru/easyjson">mailru/easyjson</a></li>
<li><a href="https://github.com/pquerna/ffjson">pquerna/ffjson</a></li>
</ul>
<h4 id="json-iteratorgo">json-iterator/go</h4>
<p>目前社区较常用的是 json-iterator/go，我们在测试代码中用到了它</p>
<p>它的用法与标准库 100% 兼容，并且性能有较大提升。我们一起粗略的看下是怎么做到的，如下：</p>
<h5 id="reflect2">reflect2</h5>
<p>利用 <a href="https://github.com/modern-go/reflect2">modern-go/reflect2</a> 减少运行时调度开销</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">StructDescriptor</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Type</span>   <span style="color:#a6e22e">reflect2</span>.<span style="color:#a6e22e">Type</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Fields</span> []<span style="color:#f92672">*</span><span style="color:#a6e22e">Binding</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Binding</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">levels</span>    []<span style="color:#66d9ef">int</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Field</span>     <span style="color:#a6e22e">reflect2</span>.<span style="color:#a6e22e">StructField</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">FromNames</span> []<span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">ToNames</span>   []<span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Encoder</span>   <span style="color:#a6e22e">ValEncoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Decoder</span>   <span style="color:#a6e22e">ValDecoder</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Extension</span> <span style="color:#66d9ef">interface</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">UpdateStructDescriptor</span>(<span style="color:#a6e22e">structDescriptor</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">StructDescriptor</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">CreateMapKeyDecoder</span>(<span style="color:#a6e22e">typ</span> <span style="color:#a6e22e">reflect2</span>.<span style="color:#a6e22e">Type</span>) <span style="color:#a6e22e">ValDecoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">CreateMapKeyEncoder</span>(<span style="color:#a6e22e">typ</span> <span style="color:#a6e22e">reflect2</span>.<span style="color:#a6e22e">Type</span>) <span style="color:#a6e22e">ValEncoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">CreateDecoder</span>(<span style="color:#a6e22e">typ</span> <span style="color:#a6e22e">reflect2</span>.<span style="color:#a6e22e">Type</span>) <span style="color:#a6e22e">ValDecoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">CreateEncoder</span>(<span style="color:#a6e22e">typ</span> <span style="color:#a6e22e">reflect2</span>.<span style="color:#a6e22e">Type</span>) <span style="color:#a6e22e">ValEncoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">DecorateDecoder</span>(<span style="color:#a6e22e">typ</span> <span style="color:#a6e22e">reflect2</span>.<span style="color:#a6e22e">Type</span>, <span style="color:#a6e22e">decoder</span> <span style="color:#a6e22e">ValDecoder</span>) <span style="color:#a6e22e">ValDecoder</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">DecorateEncoder</span>(<span style="color:#a6e22e">typ</span> <span style="color:#a6e22e">reflect2</span>.<span style="color:#a6e22e">Type</span>, <span style="color:#a6e22e">encoder</span> <span style="color:#a6e22e">ValEncoder</span>) <span style="color:#a6e22e">ValEncoder</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h5 id="struct-encoderdecoder-cache">struct Encoder/Decoder Cache</h5>
<p>类型为 struct 时，只需要反射一次 Name 和 Type，会缓存 struct Encoder 和 Decoder</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">typeDecoders</span> = <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#a6e22e">ValDecoder</span>{}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">fieldDecoders</span> = <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#a6e22e">ValDecoder</span>{}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">typeEncoders</span> = <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#a6e22e">ValEncoder</span>{}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">fieldEncoders</span> = <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#a6e22e">ValEncoder</span>{}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">extensions</span> = []<span style="color:#a6e22e">Extension</span>{}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">...</span>.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">fieldNames</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">calcFieldNames</span>(<span style="color:#a6e22e">field</span>.<span style="color:#a6e22e">Name</span>(), <span style="color:#a6e22e">tagParts</span>[<span style="color:#ae81ff">0</span>], <span style="color:#a6e22e">tag</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">fieldCacheKey</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Sprintf</span>(<span style="color:#e6db74">&#34;%s/%s&#34;</span>, <span style="color:#a6e22e">typ</span>.<span style="color:#a6e22e">String</span>(), <span style="color:#a6e22e">field</span>.<span style="color:#a6e22e">Name</span>())
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">decoder</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">fieldDecoders</span>[<span style="color:#a6e22e">fieldCacheKey</span>]
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> <span style="color:#a6e22e">decoder</span> <span style="color:#f92672">==</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">decoder</span> = <span style="color:#a6e22e">decoderOfType</span>(<span style="color:#a6e22e">ctx</span>.append(<span style="color:#a6e22e">field</span>.<span style="color:#a6e22e">Name</span>()), <span style="color:#a6e22e">field</span>.<span style="color:#a6e22e">Type</span>())
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">encoder</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">fieldEncoders</span>[<span style="color:#a6e22e">fieldCacheKey</span>]
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> <span style="color:#a6e22e">encoder</span> <span style="color:#f92672">==</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">encoder</span> = <span style="color:#a6e22e">encoderOfType</span>(<span style="color:#a6e22e">ctx</span>.append(<span style="color:#a6e22e">field</span>.<span style="color:#a6e22e">Name</span>()), <span style="color:#a6e22e">field</span>.<span style="color:#a6e22e">Type</span>())
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h5 id="文本解析优化">文本解析优化</h5>
<h4 id="小结-1">小结</h4>
<p>相较于官方标准库，第三方库 <code>json-iterator/go</code> 在运行时上做的更好。这是它快的原因</p>
<p>有个需要注意的点，在 Go1.10 后 <code>map</code> 类型与标准库的已经没有太大的性能差异。但是，例如 <code>struct</code> 类型等仍然有较大的性能提高</p>
<h2 id="总结">总结</h2>
<p>在本文中，我们首先进行了性能测试，再分析了不同方案，得知为什么了快慢的原因。那么最终在选择方案时，可以根据不同的应用场景去抉择：</p>
<ul>
<li>对性能开销有较高要求：选用 <code>for</code>，开销最小</li>
<li>中规中矩：选用 <code>for range</code>，大对象慎用</li>
<li>量小、占用小、数量可控：选用 <code>json.Marshal/Unmarshal</code> 的方案也可以。其<strong>重复代码</strong>少，但开销最大</li>
</ul>
<p>在绝大多数场景中，使用哪种并没有太大的影响。但作为工程师你应当清楚其利弊。以上就是不同的方案<strong>分析概要</strong>，希望对你有所帮助 :)</p>


                                        
                                        
                                        
                                        <div class="rp4wp-related-posts">
                                            <h3>相关文章</h3>
                                            <ul>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/talk/2019-01-20-control-goroutine/">来，控制一下 goroutine 的并发数量</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/slice/2019-01-06-why-slice-max/">Go Slice 最大容量大小是怎么来的</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/talk/2018-12-26-go-memory-align/">在 Go 中恰到好处的内存对齐</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/pkg/2018-12-15-unsafe/">有点不安全却又一亮的 Go unsafe.Pointer</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/slice/2018-12-11-slice/">深入理解 Go Slice</a>
                                                    </div>
                                                </li>
                                                
                                            </ul>
                                        </div>
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        <div class="post-tags">
                                            <ul>
                                            
                                            <li>
                                                <a href="/tags/go" title="View all posts tagged match">go</a>
                                            </li>
                                            
                                            </ul>
                                        </div>
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.asia/posts/posts/go/talk/2019-01-20-control-goroutine/"
                                                    rel="prev">来，控制一下 goroutine 的并发数量</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.asia/posts/posts/go/map/2019-03-05-map-access/">深入理解 Go map：初始化和访问元素</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.asia/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    <section id="qrcode" class="widget widget_media_image">
        <h2 class="widget-title">微信公众号</h2>
        <img width="258" height="258"
            src="https://www.lican.asia/wechat/lican.png"
            class="image wp-image-5514  attachment-full size-full" alt=""
            style="max-width: 100%; height: auto;"
            sizes="(max-width: 258px) 100vw, 258px">
    </section>
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.asia All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>